home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / str.c < prev    next >
C/C++ Source or Header  |  1996-06-06  |  3KB  |  161 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include "misc.h"
  5.  
  6. /*
  7.     Strips all blank and line feed at the end of a string
  8.       newstr and str may be the same string.
  9.     Return the number of bytes striped at the end.
  10. */
  11. int str_strip (
  12.     const char *str,    // String to truncate
  13.     char *newstr)        // result
  14. {
  15.     int ret = 0;
  16.     int len = strlen(str);
  17.     char *pt = newstr + len-1;
  18.     strcpy (newstr,str);
  19.     while (len > 0 && isspace(*pt)){
  20.         *pt-- = '\0';
  21.         len --;
  22.         ret ++;
  23.     }
  24.     return (ret);
  25. }
  26. /*
  27.     Strip all blank (white space) at the end of a line.
  28.     blanks are identified by isspace().
  29.     Return a pointer to the last character +1 (right on the '\0', the
  30.     new one).
  31.  
  32.     ATTENTION: To help cooperate with some DOS editor, ^Z is processed
  33.     as a white space.
  34. */
  35.  
  36. char *strip_end(char *str)
  37. {
  38.     int len = strlen(str);
  39.     for (str += len - 1
  40.         ; len > 0 && (isspace(*str) || *str == 26)
  41.         ; len--, str--) *str = '\0';
  42.     return str+1;
  43. }
  44. /*
  45.     Skip the white space in a string. Stop at the end or at the first
  46.     non white space.
  47. */
  48. char *str_skip(const char *str)
  49. {
  50.     while (isspace(*str)) str++;
  51.     return (char*)str;
  52. }
  53. /*
  54.     Skip the digit space in a string. Stop at the end or at the first
  55.     non digit character.
  56. */
  57. char *str_skipdig(const char *str)
  58. {
  59.     while (isdigit(*str)) str++;
  60.     return (char*)str;
  61. }
  62.  
  63. /*
  64.     Copy one word from a string. A word is a sequence of non white space.
  65.     Return a pointer on the first blank character after the word.
  66.  
  67.     If there were no word, dest[0] == '\0'
  68. */
  69.  
  70. char *str_copyword(char *dest, const char *str)
  71. {
  72.     str = str_skip(str);
  73.     while (isgraph(*str)) *dest++ = *str++;
  74.     *dest = '\0';
  75.     return (char*) str;
  76. }
  77. /*
  78.     Copy one word from a string. A word is a sequence of non white space.
  79.     Return a pointer on the first blank character after the word.
  80.  
  81.     If there were no word, dest[0] == '\0'
  82. */
  83.  
  84. char *str_copyword(SSTRING &dest, const char *str)
  85. {
  86.     str = str_skip(str);
  87.     char tmp[1000];
  88.     char *pt = tmp;
  89.     while (isgraph(*str)) *pt++ = *str++;
  90.     *pt = '\0';
  91.     dest.setfrom (tmp);
  92.     return (char*) str;
  93. }
  94.  
  95. /*
  96.     Free all entry of the string table.
  97. */
  98. void tbstr_free (char *tb[], int nb)
  99. {
  100.     for (int i=0; i<nb; i++) free (tb[i]);
  101. }
  102.  
  103. /*
  104.     Why is it missing ?
  105. */
  106. int stricmp (const char *str1, const char *str2)
  107. {
  108.     int ret = 0;
  109.     while (1){
  110.         if (*str1 == '\0'){
  111.             if (*str2 != 0){
  112.                 ret = -1;
  113.             }
  114.             break;
  115.         }else if (*str2 == '\0'){
  116.             ret = 1;
  117.             break;
  118.         }else{
  119.             int car1 = toupper(*str1);
  120.             int car2 = toupper(*str2);
  121.             ret = car1 - car2;
  122.             if (ret != 0) break;
  123.             str1++;
  124.             str2++;
  125.         }
  126.     }
  127.     return ret;
  128. }
  129.  
  130.  
  131. void strupr(char *str)
  132. {
  133.     while (*str != '\0'){
  134.         *str = toupper(*str);
  135.         str++;
  136.     }
  137. }
  138.  
  139. /*
  140.     Extract a string from a buffer.
  141.     The string may be a single word or many words enclose in
  142.     double quote.
  143. */
  144. const char *str_extract (const char *buf, SSTRING &s)
  145. {
  146.     buf = str_skip(buf);
  147.     char word[200];
  148.     if (*buf == '"'){
  149.         buf++;
  150.         char *pt = word;
  151.         while (*buf != '\0' && * buf != '"') *pt++ = *buf++;
  152.         *pt = '\0';
  153.         if (*buf == '"') buf++;
  154.     }else{
  155.         buf = str_copyword (word,buf);
  156.     }
  157.     s.setfrom (word);
  158.     return buf;
  159. }
  160.  
  161.